<template>
	<view class="container">
		<view class="register-form">
			<view class="form-item">
				<text class="label">用户名：</text>
				<input class="input" type="text" v-model="username" placeholder="请输入用户名" />
			</view>
			<view class="form-item">
				<text class="label">密码：</text>
				<input class="input" type="password" v-model="password" placeholder="请输入密码" />
			</view>
			<view class="form-item">
				<text class="label">确认密码：</text>
				<input class="input" type="password" v-model="password1" placeholder="请确认密码" />
			</view>
			<view class="form-item">
				<button class="register-button" @click="register">注册</button>
			</view>
		</view>
		<view class="login-link">
			<text @click="goToLogin">已有账号？前往登录</text>
		</view>
	</view>
</template>

<script>
	import {
		postData
	} from '@/axios/http';
	import {
		ElMessage
	} from 'element-plus';
	export default {
		data() {
			return {
				username: '',
				password: '',
				password1: ''
			}
		},
		methods: {
			goToLogin() {
				// 跳转到登录页面
				uni.navigateTo({
					url: '/pages/login/login'
				});
			},
			async register() { //判断两次密码是否输入一致
				if (this.password != this.password1) {
					ElMessage({
						message: '两次密码输入不一致',
						offset: 250,
					});
				} else {
					let data = {
						username: this.username,
						password: this.password
					}
					try {
						const response = await postData('/register', data); // 调用封装的 POST 请求
						console.log(response.data)
						//如果注册不成功
						if (response.data.code != 200) {
							uni.showToast({
								title: response.data.message,
								icon: "error",
								duration: 2000
							});

						} else {
							uni.showModal({
								title: '注册成功',
								// content: '这是一个模态弹窗',
								success: function(res) {
									if (res.confirm) {
										// console.log('用户点击确定');
										uni.redirectTo({
											url: '/pages/login/login'
										});
									} else if (res.cancel) {
										// console.log('用户点击取消');
									}
								}
							});
						}
					} catch (error) {
						console.error('请求失败:', error);
					}
				}


			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
		background: linear-gradient(135deg, #a0c4ff, #ffffff);
		/* 添加渐变背景 */
		padding: 20px;
		flex-wrap: wrap;
		flex-direction: row-reverse;
		align-content: center;
	}

	.register-form {
		background-color: #fff;
		padding: 20px;
		border-radius: 15px;
		/* 圆角 */
		box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
		/* 阴影 */
		width: 100%;
		max-width: 400px;
		/* text-align: center; */
	}

	.form-item {
		margin-bottom: 20px;
	}

	.label {
		display: block;
		margin-bottom: 5px;
		font-size: 16px;
		color: #333;
		font-weight: bold;
	}

	.input {
		/* width: 100%; */
		padding: 12px;
		border: 1px solid #ccc;
		border-radius: 8px;
		font-size: 16px;
		color: #333;
		background-color: #f9f9f9;
	}

	.register-button {
		width: 100%;
		padding: 12px;
		background-color: #007aff;
		color: #fff;
		border: none;
		border-radius: 8px;
		/* 圆角 */
		font-size: 16px;
		cursor: pointer;
		transition: background-color 0.3s ease;
		/* 按钮动画 */
	}

	.register-button:active {
		background-color: #0056b3;
	}

	.login-link {
		margin-top: 20px;
		font-size: 14px;
		color: #007aff;
		cursor: pointer;
	}

	.login-link text {
		color: #007aff;
	}
</style>